layui.use('table', function () {
    var table = layui.table;
    //第一个实例
    table.render({
        elem: '#courseChoose-list'
        , height: 'full'
        , url: '/course/1/list' //数据接口
        , parseData: function (res) {
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": changeNature(res.data.data) //解析数据列表
            }
        }
        , request: {
            method: 'GET'
            , pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , toolbar: 'default' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            , layEvent: 'LAYTABLE_TIPS'
            , icon: 'layui-icon-tips'
        }]
        , title: '课程信息表'
        , cols: [[
            {field: 'id', title: 'ID', width: 80}
            , {field: 'courseId', title: '课程号', width: 200}
            , {field: 'courseName', title: '课程名', width: 150}
            , {field: 'teacherId', title: '教师编号', width: 200}
            , {field: 'collegeId', title: '学院', width: 100}
            , {field: 'systemId', title: '系', width: 100}
            , {field: 'credit', title: '学分', width: 80}
            , {field: 'nature', title: '属性', width: 80}
            , {field: 'number', title: '人数', width: 100, type: toolbar}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 200, align: 'center'}
        ]]
        , page: true //开启分页
    });

    function changeNature(courses) {
        for (let course of courses) {
            course.nature = '选修课';
            course.number = '<a class="my-scan layui-btn layui-btn-normal layui-btn-xs">双击查看</a>';
        }
        return courses;
    }

    //头工具栏事件
    table.on('toolbar(courseChoose-list)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    $("#body-content").load("/pages/course/courseChooseAdd");
                    $("#pages\\/courseMustList").parent().attr('class', '');
                    $("#pages\\/courseMustAdd").parent().attr('class', 'layui-this');
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        // layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                        updateCourse(checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('真的删除这些行么', function (index) {
                            for (let course of data) {
                                if (deleteCourse(course.id) !== 0) {
                                    break;
                                }
                            }
                        });
                    }
                    break;
            }
        }
    );

    // 监听行工具事件
    table.on('tool(courseChoose-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                layer.close(index);
                deleteCourse(obj.data.id);
            });
        } else if (obj.event === 'edit') {
            updateCourse(obj.data.id);
        }
    });

    table.on('rowDouble(courseChoose-list)', function (obj) {
        window.sessionStorage.setItem('courseID', obj.data.id);
        window.sessionStorage.setItem('currentCourseId', obj.data.courseId);
        layer.open({
            type: 2,
            title: '课程信息栏',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/pages/course/courseStudentList',
            cancel: function (index) {
                layer.close(index);
            }
        });
    });

    function updateCourse(id) {
        window.sessionStorage.setItem('courseID', id);
        layer.open({
            type: 2,
            title: '课程信息栏',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/pages/course/courseChooseUpdate',
            cancel: function (index) {
                layer.close(index);
            }
        });
    }

    function deleteCourse(id) {
        let code = 0;
        $.ajax({
            url: '/course/delete/' + id,
            type: 'GET',
            success: function (res) {
                if (res.code === 0) {
                    layer.msg("删除" + id + "成功！");
                } else {
                    layer.msg("删除" + id + "失败！");
                }
                code = res.code;
            }
        })
        return code;
    }
})
;

